home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / DIBITMAP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.8 KB  |  277 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.12  $
  6. //
  7. // Implementation of TDiBitmap class, a combination of a GDI TBitmap object and
  8. // a Device Independent Bitmap TDib object.
  9. //----------------------------------------------------------------------------
  10. #pragma hdrignore SECTION
  11. #include <owl/pch.h>
  12. #if !defined(OWL_GDIOBJEC_H)
  13. # include <owl/gdiobjec.h>
  14. #endif
  15. #if !defined(OWL_DIBITMAP_H)
  16. # include <owl/dibitmap.h>
  17. #endif
  18. #if !defined(OWL_WING_H) && !defined(BI_PLAT_WIN32)
  19. # include <owl/wing.h>
  20. #endif
  21.  
  22. OWL_DIAGINFO;
  23.  
  24. //
  25. // Initialize our bitmap side from the dib side. TDib::Bits gets setup to
  26. // point to the new DIBSection/WinG managed dib buffer.
  27. //
  28. void TDiBitmap::InitBitmap()
  29. {
  30. //  PRECONDITION(Bits == 0);
  31.  
  32.   TScreenDC dc;
  33.   TPalette p(*(TDib*)this);
  34.   dc.SelectObject(p);
  35.   dc.RealizePalette();
  36. #if defined(BI_PLAT_WIN32)
  37.   void *bits;
  38.   TBitmap::Handle = dc.CreateDIBSection(*GetInfo(), Usage(), &bits);
  39.   memcpy(bits, Bits, SizeImage());
  40.   Bits = bits;
  41. #else
  42.   if (TWinG::IsAvailable())
  43.     TBitmap::Handle = TWinG::Dll()->CreateBitmap(dc, GetInfo(), &(void far*)Bits);
  44. #endif
  45.   TBitmap::CheckValid();
  46.   TBitmap::RefAdd(TBitmap::Handle, Bitmap);
  47. }
  48.  
  49. //    TWinGBitmap(TWinGIdentity&, TDibDC& dc, const TSize& size);
  50. //    TWinGBitmap(TWinGIdentity&, TDibDC& dc, TDib& dib);
  51. //    TWinGBitmap(TWinGIdentity&, TDibDC& dc, int width, int height);
  52.  
  53.  
  54. //
  55. // Construct from existing bitmap handle.
  56. //
  57. TDiBitmap::TDiBitmap(HGLOBAL handle, TAutoDelete autoDelete)
  58. :
  59.   TDib(handle, autoDelete)
  60. {
  61.   InitBitmap();
  62. }
  63.  
  64. //
  65. // Construct from bitmap stored in clipboard.
  66. //
  67. TDiBitmap::TDiBitmap(const TClipboard& clipboard)
  68. :
  69.   TDib(clipboard)
  70. {
  71.   InitBitmap();
  72. }
  73.  
  74. //
  75. // Construct a copy from existing TDib.
  76. //
  77. TDiBitmap::TDiBitmap(const TDib& src)
  78. :
  79.   TDib(src)
  80. {
  81.   InitBitmap();
  82. }
  83.  
  84. //
  85. // Construct new bitmap with the passed-in parameters.
  86. //
  87. TDiBitmap::TDiBitmap(int width, int height, int nColors, uint16 mode)
  88. :
  89.   TDib(width, height, nColors, mode)
  90. {
  91.   InitBitmap();
  92. }
  93.  
  94. //
  95. // Construct a bitmap stored as resource.
  96. //
  97. TDiBitmap::TDiBitmap(HINSTANCE module, TResId resid)
  98. :
  99.   TDib(module, resid)
  100. {
  101.   InitBitmap();
  102. }
  103.  
  104. //
  105. // Construct from DIB stored as a file.
  106. //
  107. TDiBitmap::TDiBitmap(const char* name)
  108. :
  109.   TDib(name)
  110. {
  111.   InitBitmap();
  112. }
  113.  
  114. //
  115. // Construct from a TFile.
  116. //
  117. TDiBitmap::TDiBitmap(TFile& file, bool readFileHeader)
  118. :
  119.   TDib(file, readFileHeader)
  120. {
  121.   InitBitmap();
  122. }
  123.  
  124. #if !defined(BI_DATA_NEAR)
  125. //
  126. // Construct from input stream.
  127. //
  128. TDiBitmap::TDiBitmap(istream& is, bool readFileHeader)
  129. :
  130.   TDib(is, readFileHeader)
  131. {
  132.   InitBitmap();
  133. }
  134. #endif
  135.  
  136. //
  137. // Construct from a bitmap and palette information.
  138. //
  139. TDiBitmap::TDiBitmap(const TBitmap& bitmap, const TPalette* pal)
  140. :
  141.   TDib(bitmap, pal)
  142. {
  143.   InitBitmap();
  144. }
  145.  
  146. //
  147. // Restore Bits data member to 0.
  148. //
  149. TDiBitmap::~TDiBitmap()
  150. {
  151.   Bits = 0;  // Make sure that TDib doesn't mess with our unusual bits
  152. }
  153.  
  154. //----------------------------------------------------------------------------
  155.  
  156. // Implementation of (WinG) Identity Palette object, TIdentityPalette.
  157. // Use of an identity palette allows objects to be blitted without having
  158. // the color mapper come into play.  The overall effect is faster graphics.
  159.  
  160. //
  161. // Return an identity palette to be used by bitmaps.  This ensures
  162. // speed and color accuracy.
  163. //
  164. TIdentityPalette::TIdentityPalette(const TPalette& palette)
  165. :
  166.   TPalette(0, NoAutoDelete)
  167. {
  168.   TScreenDC sdc;
  169.  
  170.   int nColors = palette.GetNumEntries();
  171.   int nSysColors = sdc.GetDeviceCaps(SIZEPALETTE);
  172.  
  173.   LOGPALETTE* logPal = (LOGPALETTE*)new uint8[
  174.      sizeof(LOGPALETTE)+(nColors-1)*sizeof(PALETTEENTRY) ];
  175.   logPal->palVersion  = 0x300;
  176.   logPal->palNumEntries = (uint16)nColors;
  177.  
  178.   if (sdc.GetSystemPaletteUse() == SYSPAL_NOSTATIC) {
  179.     int colorIndex;
  180.  
  181.     // Set the entries to PC_NOCOLLAPSE
  182.     //
  183.     palette.GetPaletteEntries(0, uint16(nColors), logPal->palPalEntry);
  184.     for (colorIndex=0; colorIndex < nColors; colorIndex++)
  185.       logPal->palPalEntry[colorIndex].peFlags = PC_NOCOLLAPSE;
  186.  
  187.     // Make the first entry black
  188.     //
  189.     logPal->palPalEntry[0].peRed   = 0;
  190.     logPal->palPalEntry[0].peGreen = 0;
  191.     logPal->palPalEntry[0].peBlue  = 0;
  192.     logPal->palPalEntry[0].peFlags = 0;
  193.  
  194.     // Make the last entry white
  195.     //
  196.     logPal->palPalEntry[255].peRed   = 255;
  197.     logPal->palPalEntry[255].peGreen = 255;
  198.     logPal->palPalEntry[255].peBlue  = 255;
  199.     logPal->palPalEntry[255].peFlags = 0;
  200.   }
  201.   else {
  202.     // SystemPaletteUse == SYSPAL_STATIC
  203.     //
  204.     int staticColors = sdc.GetDeviceCaps(NUMCOLORS) / 2;
  205.     int usableColors = nColors - staticColors;
  206.  
  207.     //    the offset of the systems static palette use.
  208.     //
  209.     sdc.GetSystemPaletteEntries(0, staticColors, logPal->palPalEntry);
  210.     palette.GetPaletteEntries(0, uint16(usableColors), logPal->palPalEntry+staticColors);
  211.     sdc.GetSystemPaletteEntries(nSysColors - staticColors, staticColors,
  212.                                 logPal->palPalEntry + nSysColors - staticColors);
  213.  
  214.     int colorIndex = 0;
  215.     for (; colorIndex < staticColors; colorIndex++)
  216.       logPal->palPalEntry[colorIndex].peFlags = 0;
  217.     for (; colorIndex < nColors; colorIndex++)
  218.       logPal->palPalEntry[colorIndex].peFlags = PC_NOCOLLAPSE;
  219.     for (; colorIndex < nSysColors; colorIndex++)
  220.       logPal->palPalEntry[colorIndex].peFlags = 0;
  221.   }
  222.   Handle = ::CreatePalette(logPal);
  223.   delete[] logPal;
  224.  
  225.   ShouldDelete = true;
  226.   CheckValid();
  227.   RefAdd(Handle, Palette);
  228. }
  229.  
  230. //
  231. // Default constructor. In 32-bits, retrieves the system's halftone palette.
  232. // In 16-bits, asks WinG for its halftone palette.
  233. //
  234. THalftonePalette::THalftonePalette()
  235. :
  236. #if defined(BI_PLAT_WIN32)
  237.   TPalette(::CreateHalftonePalette(TScreenDC()), NoAutoDelete)
  238. #else
  239.   TPalette(TWinG::IsAvailable() ? TWinG::Dll()->CreateHalftonePalette() : 0,
  240.            NoAutoDelete )
  241. #endif
  242. {
  243.  
  244.   ShouldDelete = true;
  245.   CheckValid();
  246.   RefAdd(Handle, Palette);
  247. }
  248.  
  249. //
  250. // Force the system palette to a known state.  This state will be
  251. // an empty palette.
  252. //
  253. /*
  254. void
  255. TWinGIdentity::ClearSystemPalette()
  256. {
  257.   TWinGLogPalette palette = { PaletteVersion, SupportedColors };
  258.  
  259.   // Reset everything in the system palette to black
  260.   //
  261.   for (int counter = 0; counter < SupportedColors; counter++) {
  262.     palette.Entries[counter].peRed   = 0;
  263.     palette.Entries[counter].peGreen = 0;
  264.     palette.Entries[counter].peBlue  = 0;
  265.     palette.Entries[counter].peFlags = PC_NOCOLLAPSE;
  266.   }
  267.  
  268.   // Create, select, realize, deselect, and delete the palette.
  269.   //
  270.   TScreenDC sdc;
  271.   TPalette sp((LOGPALETTE*)&palette);
  272.  
  273.   sdc.SelectObject(sp, false);
  274.   sdc.RealizePalette();
  275. }
  276. */
  277.